<?php

namespace app\home\controller;

use think\Controller;
use think\Request;
use app\home\model\Cart as Carts;
use app\home\model\GoodsSku;
use app\home\model\SkuKey;
use app\home\model\SkuValue;

class Cart extends Controller
{   
    protected $middleware = [
        'Check'=> ['except' 	=> ['index1'] ],
    ];
    //往购物车中添加商品
    public function add_goods(Request $req)
    {   
        //先查询该商品是否已经存在购物车数据表中
        $info = db('cart')->where('sku_id',$req->sku_id)->find();
        //若不存在 
        if($info==null){
            //把sku_id作为where条件去goods_sku表查询sku信息
            //select a.*,b.* from goods_sku a left join goods b on a.goods_id = b.spu_id where a.id = 100000000026
            $list = db('goods_sku')->where('a.id',$req->sku_id)->alias('a')->field('a.*,b.*')->join('goods b','a.goods_id=b.spu_id')->find();
            //然后再把sku属性名和sku属性值的id作为条件去关联sku_key表和sku_value表  $req->attr;
            // var_dump($req->attr);die;
            $a = '';
            for($i=0;$i<count($req->attr);$i++){
                $arr = explode(':',$req->attr[$i]);
                $str1 = db('sku_key')->where('id',$arr[0])->field('sku_key')->select();
                $str2 = db('sku_value')->where('id',$arr[1])->field('sku_value')->select();
                $str = $str1[0]['sku_key'].':'. $str2[0]['sku_value'];
                $a .= $str.' ';
            }
            //商品sku属性
            $a = trim($a);
            $data = ['sku_id'=>$req->sku_id,'cart_goods_num'=>$req->cart_num,'cart_goods_name'=>$list['goods_name'],'cart_goods_img'=>$list['images'],'cart_sku_attr'=>$a,'cart_goods_price'=>$list['sku_price']];
            db('cart')->strict(false)->insert($data);
            return 1;
            // var_dump($list);die;

        }else{//若存在 向购物车追加数量
            $addNum = $req->cart_num;
            $total_num = $info['cart_goods_num']+$addNum;
            // $data = ['cart_goods_num'=>$toal_num];
            db('cart')->where('sku_id',$req->sku_id)->setField('cart_goods_num',$total_num);

            return 2;
        }
    }

    //给已经加入购物车的商品添加数量
    public function addNum(Request $req)
    {
        // return $req->id;
        db('cart')->where('id',$req->id)->inc('cart_goods_num')->update();
        $a = db('cart')->where('id',$req->id)->find();

        $data['num'] = $a['cart_goods_num'];
        $data['price']= $a['cart_goods_num']*$a['cart_goods_price'];
        return $data;
    }
    //给已经加入购物车的商品减少数量
    public function delNum(Request $req)
    {
        // return $req->id;
        db('cart')->where('id',$req->id)->dec('cart_goods_num')->update();
        $a = db('cart')->where('id',$req->id)->find();

        $data['num'] = $a['cart_goods_num'];
        $data['price']= $a['cart_goods_num']*$a['cart_goods_price'];
        return $data;
    }
    
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        // return $id;
        Carts::destroy(1);
        return true;
    }
}
